package com.tools.cleanmaster.ui.view.circle;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.DrawFilter;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.PaintFlagsDrawFilter;
import android.graphics.RadialGradient;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.Shader.TileMode;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.widget.FrameLayout;

import com.tools.cleanmaster.R;
import com.tools.cleanmaster.utils.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;

@SuppressWarnings("static-access")
public class TrashGreatCircleView extends FrameLayout {

    private static final int[] Star_Color = new int[]{
            0xFFFFFFFF, 0x00FFFFFF
    };
    private static ArrayList<StarBean> starBeans = new ArrayList<StarBean>();
    private static HashSet<StarBean> drawSet = new HashSet<StarBean>();
    private static final int[] Stars_Points = new int[]{
            132, 316,
            115, 360,
            195, 430,
            450, 342,
            438, 278,
            455, 212,
            408, 168,
            342, 120,
            249, 88,
            244, 124,
            108, 190
    };
    private int mStarSize;

    /**
     * 绘制星星产生需要的类
     *
     * @author thomas
     */
    private static class StarBean {
        public float x;
        public float y;
        public float radius;
        public int flash;
        public int level;
    }

    private Paint mPaint;
    private Paint mTextSizePaint;
    private Paint mTextUnitPaint;
    private Paint mTextWordPaint;
    private int mProgressLength;
    private Paint mShaderPaint;
    private Paint mShaderLinePaint;
    private float mTotalWidth, mHalfWidth;
    private int mTotalHeight, mHalfHeight;

    // 弧形扫描条颜色值
    private int mShaderColor = 0x9900f6ff;
    private boolean isScan;
    private boolean isClean;
    private boolean isCleanEnd;
    private int mInnerCircle_progress;
    private int mInnerCircle;
    private int mCleanInnerCircleWidth;
    private int mShaderRingWidth;
    private Shader mProgressShader;
    private Matrix mMatrix;
    private RectF mShaderOval;
    private int mShaderRaduls;
    private Paint mStarPaint;
    private int mProgressRad;
    private int mInnerProgressLength;
    private int mShaderSpeed = 3;
    private long mTrashSize;
    private int mTrashSizeUnitMarginLeft;
    private int mTrashCleanUpSizeCenter;
    private String mCleanUP;
    private int mProgressWidth;
    private Paint mCleanInnerCirclepaint;
    private int mOneDP;
    private int mOuterCircle;
    private TrashGreatCircleListener mTrashGreatCircleListener;
    private int mCleanSpeed = 5;
    private int mRescanSpeed = 10;
    private RectF mProgressOval;
    private Paint mProgressPaint;
    private long mCleanSize;
    private boolean isFinish;
    private boolean isNeedCleanAgain;
    private Paint mTextExcellentPaint;
    private int mCleanTextSizeMarginTop;
    private int mScanCommentTextMarginTop;
    private DrawFilter mCanvasFilter;
    private boolean isReScan;

    private long INVALIDATE_DELAYED = 10L;

    /**
     * 构造方法
     */
    public TrashGreatCircleView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    /**
     * 构造方法
     */
    public TrashGreatCircleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    /**
     * 构造方法
     */
    public TrashGreatCircleView(Context context) {
        super(context);
        init(context);
    }

    /**
     * 设置外圈进度条
     *
     * @param length 该进度条范围在0~360之间，大于或小于范围将不生效。
     */
    public void setProgressLength(int length) {
        if (length >= 0 && length <= 360) {
            mProgressLength = length;
        } else if (length < 0) {
            mProgressLength = 0;
        } else if (length > 360) {
            mProgressLength = 360;
        }
    }

    /**
     * 设置显示的垃圾大小
     *
     * @param size 当size大小小于0时，设置将不会生效。
     */
    public void setTrashSize(long size) {
        if (size >= 0) {
            mTrashSize = size;
        }
    }

    /**
     * 设置重新扫描外进度条速度
     *
     * @param speed 当速度小于0时，将不生效。
     */
    public void setRescanSpeed(int speed) {
        if (speed >= 0) {
            mRescanSpeed = speed;
        }
    }

    /**
     * 初始化view方法，初始化各种paint.
     */
    private void init(Context con) {
        // 初始化内圆半径
        mInnerCircle = getResources().getDimensionPixelSize(R.dimen.circle_view_inner_radius);
        mCleanInnerCircleWidth = getResources().getDimensionPixelSize(R.dimen.circle_view_clean_inner_circle_width);
        mShaderRingWidth = getResources().getDimensionPixelSize(R.dimen.circle_view_progress_shader_width);
        mTrashSizeUnitMarginLeft = getResources()
                .getDimensionPixelSize(R.dimen.circle_view_trash_size_unit_margin_left);
        mTrashCleanUpSizeCenter = getResources()
                .getDimensionPixelSize(R.dimen.circle_view_trash_clean_up_size_center);
        mProgressWidth = getResources().getDimensionPixelSize(R.dimen.circle_view_trash_progress_circle_width);
        mProgressRad = getResources().getDimensionPixelSize(R.dimen.circle_view_trash_out_progress_rad);
        mShaderRaduls = getResources().getDimensionPixelSize(R.dimen.circle_view_shader_progress_shader_rad);
        mOneDP = getResources().getDimensionPixelSize(R.dimen.circle_view_trash_one_dp);
        mOuterCircle = getResources().getDimensionPixelSize(R.dimen.circle_view_clean_out_circle_rad);
        mCleanTextSizeMarginTop = getResources()
                .getDimensionPixelSize(R.dimen.circle_view_clean_trash_text_size_margin_top);
        mScanCommentTextMarginTop = getResources()
                .getDimensionPixelSize(R.dimen.circle_view_clean_trash_comment_text_margin_top);
        mStarSize = (int) getResources().getDimension(R.dimen.circle_view_start_width);
        mCleanUP = getResources().getString(R.string.circle_view_clean_up);
        mInnerCircle_progress = mInnerCircle;
        mCanvasFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
        // 数字画笔
        mPaint = new Paint();
        mPaint.setAntiAlias(true); // 消除锯齿
        mPaint.setStyle(Paint.Style.STROKE); // 绘制空心圆
        // 扇形画笔
        mShaderPaint = new Paint();
        mShaderPaint.setAntiAlias(true);
        mShaderPaint.setStyle(Paint.Style.STROKE); // 绘制空心圆
        mShaderPaint.setStrokeWidth(mShaderRingWidth);
        // 扇形分割线画笔
        mShaderLinePaint = new Paint();
        mShaderLinePaint.setAntiAlias(true);
        mShaderLinePaint.setStyle(Paint.Style.STROKE); // 绘制空心圆
        mShaderLinePaint.setStrokeWidth(mShaderRingWidth);
        mShaderLinePaint.setARGB(255, 212, 225, 233);
        mMatrix = new Matrix();
        // Trash Star paint init
        mStarPaint = new Paint();
        mStarPaint.setAntiAlias(true);
        // mStarPaint.setARGB(0xff, 0xFF, 0xFF, 0xFF);
        // 外部进度条画笔
        mProgressPaint = new Paint();
        mProgressPaint.setAntiAlias(true);
        mProgressPaint.setStyle(Paint.Style.STROKE); // 绘制空心圆
        mProgressPaint.setARGB(64, 0xff, 0xff, 0xFF);
        mProgressPaint.setStrokeWidth(mProgressWidth);
        // 清理内部圆环画笔
        mCleanInnerCirclepaint = new Paint();
        mCleanInnerCirclepaint.setColor(0xff307bd7);
        mCleanInnerCirclepaint.setAntiAlias(true);

    }

    /**
     * 初始化颜色
     */
    private Shader getCircleShader() {
        int[] colors = new int[]{0x00FFFFFF, 0x00FFFFFF, mShaderColor
        };
        return new SweepGradient(mHalfWidth, mHalfHeight, colors, null);
    }

    /**
     * 设置弧形扫描条颜色
     */
    public void setShaderColor(int color) {
        if (isScan) {
            mShaderColor = color;
            mProgressShader = getCircleShader();
        }
    }

    /**
     * 扫描动画方法
     */
    private int scan(Canvas canvas) {
        int start = 270 + mInnerProgressLength * mShaderSpeed;
        if (start > 360) {
            start = start % 360;
        }
        // 设置旋转角度
        mMatrix.setRotate(start, mHalfWidth, mHalfHeight);
        mProgressShader.setLocalMatrix(mMatrix);
        // 设置阴影
        mShaderPaint.setShader(mProgressShader);
        // 绘制扫描弧
        canvas.drawArc(mShaderOval, 270, mInnerProgressLength * mShaderSpeed, false, mShaderPaint);
        // 绘制分割线
        canvas.drawArc(mShaderOval, start, 1, false, mShaderLinePaint);
        mInnerProgressLength++;
        return start;
    }

    /**
     * 设置扫描扇形旋转速度
     *
     * @param speed 默认为3
     */
    public void setShaderSpeed(int speed) {
        mShaderSpeed = speed;
    }

    /**
     * 设置清理速度的方法
     *
     * @param speed 每一帧减少进度条的弧度值。
     *              如果该值大于360度或者小于0度，则不会生效。
     */
    public void setCleanSpeed(int speed) {
        if (speed <= 360 && speed >= 0) {
            mCleanSpeed = speed;
        }
    }

    /**
     * 清理动画
     */
    private void clean(Canvas canvas) {
        if (mInnerCircle_progress < mOuterCircle) {
            // 增大半径
            mInnerCircle_progress += mShaderSpeed;
        }
        if (mInnerCircle_progress >= mOuterCircle) {
            mInnerCircle_progress = mOuterCircle;
            if (!isReScan) {
                // 绘制进外圈度条
                cleanStop(canvas);
                if (mTrashGreatCircleListener != null) {
                    mTrashGreatCircleListener.onCleanFinish();
                }
            } else {
                if (mTrashGreatCircleListener != null) {
                    mTrashGreatCircleListener.onReScanFinish();
                }
            }
            isFinish = true;
            isReScan = false;
        } else {
            invalidate();
        }
        drawOutCircle(canvas);
    }

    /**
     * 绘制大圆增大过程
     */
    private void drawOutCircle(Canvas canvas) {
        // 绘制内圆圈
        mPaint.setARGB(255, 212, 225, 233);
        mPaint.setStrokeWidth(mCleanInnerCircleWidth);
        RectF oval = new RectF(mHalfWidth - mInnerCircle_progress, mHalfHeight - mInnerCircle_progress,
                mHalfWidth + mInnerCircle_progress, mHalfHeight + mInnerCircle_progress);
        canvas.drawArc(oval, 0, 360, false, mPaint);// 小弧形
        // 绘制内部
        canvas.drawCircle(mHalfWidth, mHalfHeight, mInnerCircle_progress - mOneDP, mCleanInnerCirclepaint);
    }

    /**
     * 再次清理动画
     */
    private void cleanAgain(Canvas canvas) {
        if (mInnerCircle_progress > mInnerCircle) {
            // 减小半径
            mInnerCircle_progress -= mShaderSpeed;
        }
        if (mInnerCircle_progress <= mInnerCircle) {
            mInnerCircle_progress = mInnerCircle;
            // 绘制进外圈度条
            if (mTrashGreatCircleListener != null) {
                mTrashGreatCircleListener.onCleanAgainFinish();
            }
            setProgressLength(360);
            updateOutProgress(canvas, mProgressLength);
        } else {
            // 绘制内圆圈
            mPaint.setARGB(255, 212, 225, 233);
            mPaint.setStrokeWidth(mCleanInnerCircleWidth);
            RectF oval = new RectF(mHalfWidth - mInnerCircle_progress, mHalfHeight - mInnerCircle_progress,
                    mHalfWidth + mInnerCircle_progress, mHalfHeight + mInnerCircle_progress);
            canvas.drawArc(oval, 0, 360, false, mPaint);// 小弧形
            // 绘制内部
            canvas.drawCircle(mHalfWidth, mHalfHeight, mInnerCircle_progress - mOneDP, mCleanInnerCirclepaint);
           invalidate();
        }
    }

    /**
     * 开始清理动画
     */
    public void startScan() {
        isScan = true;
        isClean = false;
        isCleanEnd = false;
        isFinish = false;
        isNeedCleanAgain = false;
        invalidate();
    }

    /**
     * 开始清理动画
     *
     * @param cleanListener 清理动画完成的回调函数
     */
    public void startClean(TrashGreatCircleListener cleanListener) {
        mCleanSize = mTrashSize;
        isScan = false;
        isClean = true;
        isCleanEnd = false;
        isFinish = false;
        isNeedCleanAgain = false;
        mTrashGreatCircleListener = cleanListener;
        invalidate();
    }

    /**
     * 开始清理动画
     */
    public void whenFinsh() {
        isScan = false;
        isClean = false;
        isCleanEnd = false;
        isFinish = true;
        invalidate();
    }

    /**
     * 需要再次清理动画
     */
    public void needCleanAgain() {
        isScan = false;
        isClean = false;
        isCleanEnd = false;
        isFinish = false;
        isNeedCleanAgain = true;
        invalidate();
    }

    /**
     * 再次扫描方法，将清除扫描进度并设置进度为0
     */
    public void rescan(TrashGreatCircleListener cleanListener) {
        isScan = false;
        isClean = false;
        isCleanEnd = false;
        isFinish = false;
        isNeedCleanAgain = false;
        isReScan = true;
        mProgressLength = 0;
        mInnerCircle_progress = mInnerCircle;
        mTrashGreatCircleListener = cleanListener;
        invalidate();
    }

    /**
     * 清理完成的动画
     */
    private void cleanStop(Canvas canvas) {
        // 两个小圆弧
        mPaint.setARGB(255 / 4, 0xff, 0xff, 0xFF);
        mPaint.setStrokeWidth(mProgressWidth);
        RectF oval = new RectF(mHalfWidth - mProgressRad, mHalfHeight - mProgressRad, mHalfWidth + mProgressRad,
                mHalfHeight + mProgressRad);
        canvas.drawArc(oval, 180, 90, false, mPaint);// 小弧形
        canvas.drawArc(oval, 0, 90, false, mPaint);// 小弧形
    }

    /**
     * 绘制扫描过程中星星的方法
     *
     * @param start 弧形扫描栏旋转的角度
     */
    private void drawTrashStar(Canvas canvas, int start) {
        if (starBeans.size() == 0) {
            initStars(mHalfWidth);
        }
        long time = System.currentTimeMillis();
        time = time % 720;
        for (StarBean sb : starBeans) {
            switch (start / 45) {
                case 5:
                    if (sb.flash == 10) {
                        addStar2Set(sb);
                        sb.level = mStarSize;
                        sb.radius = mStarSize;
                    }
                    break;
                case 6: // 扫描到开始位置
                    if (mInnerProgressLength * mShaderSpeed > 90 && (sb.flash == 8 || sb.flash == 9)) {
                        addStar2Set(sb);
                        sb.level = mStarSize;
                        sb.radius = mStarSize;
                    }
                    break;
                case 7:
                    if (sb.flash == 7) {
                        addStar2Set(sb);
                        sb.level = mStarSize;
                        sb.radius = mStarSize;
                    }
                    break;
                case 0:
                    if (sb.flash == 5 || sb.flash == 6) {
                        addStar2Set(sb);
                        sb.level = mStarSize;
                        sb.radius = mStarSize;
                    }
                    break;
                case 1:
                    if ((sb.flash == 3 || sb.flash == 4)) {
                        addStar2Set(sb);
                        sb.level = mStarSize;
                        sb.radius = mStarSize;
                    }
                    break;
                case 3:
                    if (sb.flash == 2 && sb.level == 4) {
                        addStar2Set(sb);
                        sb.level = mStarSize;
                    }
                    break;
                case 4:
                    if ((sb.flash == 1 || sb.flash == 0)) {
                        addStar2Set(sb);
                        sb.level = mStarSize;
                        sb.radius = mStarSize;
                    }
                    break;
            }
        }

        for (StarBean sb : drawSet) {
            drawStar(canvas, sb);
        }
    }

    /**
     * 初始化 星星点阵坐标。 1.已圆中心点为中点，将圆进行四等分，每个象限都分布10个点。
     */
    private void initStars(float center) {
        float per = center / 275;
        StarBean sBean = null;
        for (int i = 0, len = Stars_Points.length / 2; i < len; i++) {
            sBean = new StarBean();
            sBean.x = Stars_Points[i * 2] * per;
            sBean.y = Stars_Points[i * 2 + 1] * per;
            sBean.radius = mStarSize;
            sBean.flash = i;
            sBean.level = mStarSize;
            starBeans.add(sBean);
        }
    }

    /**
     * 添加需刷新的star到set中
     */
    private void addStar2Set(StarBean starBean) {
        drawSet.add(starBean);
    }

    /**
     * 根据StarBean绘制Star
     */
    private void drawStar(Canvas canvas, StarBean starBean) {
        if (starBean.level == 0) {
            return;
        }
        Shader radialGradient = new RadialGradient(starBean.x, starBean.y, starBean.radius, Star_Color, null,
                TileMode.REPEAT);
        mStarPaint.setShader(radialGradient);
        canvas.drawCircle(starBean.x, starBean.y, starBean.radius - 1, mStarPaint);
        starBean.level = starBean.level - 1;
        starBean.radius = starBean.radius - 1;
    }

    /**
     * 绘制垃圾大小及文字
     */
    private void drawTrashSize(Canvas canvas, long size) {
        initTextPaint();
        String[] sizeUnitString = StringUtils.getSizeUnitString(size);
        // 扫描过程
        if (isScan || isClean || isNeedCleanAgain || isReScan) {
            int[] textSize = getTextWidthHeight(mTextSizePaint, sizeUnitString[0]);
            if (isClean) {
                mCleanSize = size * mProgressLength / 360l;
                sizeUnitString = StringUtils.getSizeUnitString(mCleanSize);
                canvas.drawText(sizeUnitString[0], mHalfWidth, mHalfWidth + textSize[1] / 2, mTextSizePaint);
            } else {
                // 绘制垃圾大小
                canvas.drawText(sizeUnitString[0], mHalfWidth, mHalfWidth + textSize[1] / 2, mTextSizePaint);
            }
            // 绘制单位
            canvas.drawText(sizeUnitString[1], mHalfWidth + mTrashSizeUnitMarginLeft, mHalfWidth + textSize[1] / 2,
                    mTextUnitPaint);
            // 绘制说明
            canvas.drawText(getResources().getString(isClean ? R.string.trash_cleaning_text :
                            R.string.circle_view_trash_found), mHalfWidth,
                    mHalfWidth + textSize[1] + mScanCommentTextMarginTop, mTextUnitPaint);
        } else if (isCleanEnd) {
            float fontHeight = getFontHeight(mTextExcellentPaint);
            canvas.drawText(getResources().getString(R.string.circle_view_excellent), mHalfWidth,
                    mHalfWidth + fontHeight / 4, mTextExcellentPaint);
            // 绘制单位
            int[] cleanUp = getTextWidthHeight(mTextUnitPaint, mCleanUP);
            int[] textSize = getTextWidthHeight(mTextWordPaint, sizeUnitString[0] + sizeUnitString[1]);
            int cleanUpW = cleanUp[0];
            int textSizeW = textSize[0];
            int centerW = (cleanUpW + textSizeW) / 2 - cleanUpW;
            canvas.drawText(mCleanUP, mHalfWidth - cleanUpW / 2 + centerW / 4,
                    mHalfWidth + fontHeight / 2 + mCleanTextSizeMarginTop, mTextUnitPaint);
            canvas.drawText(sizeUnitString[0] + sizeUnitString[1],
                    mHalfWidth + mTrashCleanUpSizeCenter + textSizeW / 2 + centerW / 4,
                    mHalfWidth + fontHeight / 2 + mCleanTextSizeMarginTop, mTextWordPaint);
        }
    }

    /**
     * 初始化文字画笔
     */
    private void initTextPaint() {
        if (mTextSizePaint == null) {
            // 数字画笔
            mTextSizePaint = new Paint();
            mTextSizePaint.setColor(0xffffffff);
            mTextSizePaint.setStrokeWidth(0);
            mTextSizePaint
                    .setTextSize(getResources().getDimensionPixelSize(R.dimen.circle_view_scan_trash_text_size));
            mTextSizePaint.setTextAlign(Paint.Align.CENTER);
            // 单位画笔
            mTextUnitPaint = new Paint();
            mTextUnitPaint.setColor(0xffffffff);
            mTextUnitPaint.setStrokeWidth(0);
            mTextUnitPaint
                    .setTextSize(getResources().getDimensionPixelSize(R.dimen.circle_view_scan_trash_text_unit));
            mTextUnitPaint.setTextAlign(Paint.Align.CENTER);
            // 清理完成画笔
            mTextWordPaint = new Paint();
            mTextWordPaint.setColor(0xfffee901);
            mTextWordPaint.setStrokeWidth(0);
            mTextWordPaint.setTextSize(
                    getResources().getDimensionPixelSize(R.dimen.circle_view_clean_trash_text_size_and_unit));
            mTextWordPaint.setTextAlign(Paint.Align.CENTER);
            //清理完成结果页Excellent画笔
            mTextExcellentPaint = new Paint();
            mTextExcellentPaint.setColor(0xffffffff);
            mTextExcellentPaint.setStrokeWidth(0);
            mTextExcellentPaint.setTextSize(
                    getResources().getDimensionPixelSize(R.dimen.circle_view_clean_trash_text_excellent));
            mTextExcellentPaint.setTextAlign(Paint.Align.CENTER);
        }

    }

    private void updateOutProgress(Canvas canvas, int progress) {
        // 绘制进外圈度条
        canvas.drawArc(mProgressOval, 270, progress, false, mProgressPaint);// 小弧形
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        canvas.setDrawFilter(mCanvasFilter);
        super.onDraw(canvas);
        if (isScan) { // 扫描
            // 绘制进度条
            updateOutProgress(canvas, mProgressLength);
            // 刷新
            if (mProgressLength != 360) {
                // 绘制星星
                drawTrashStar(canvas, scan(canvas));
                invalidate();
            }
        } else if (isClean) {
            // 清理过程
            if (mProgressLength <= 360 && mProgressLength >= 0) {
                if (mProgressLength != 0) {
                    // 更新进度条
                    updateOutProgress(canvas, mProgressLength);
                    // 更新进度
                    setProgressLength(mProgressLength - mCleanSpeed);
                } else {
                    isClean = false;
                    isCleanEnd = true;
                }
                if (mTrashGreatCircleListener != null) {
                    mTrashGreatCircleListener.onCleanProgress(mProgressLength);
                }
                postInvalidateDelayed(INVALIDATE_DELAYED);
            }
        } else if (isCleanEnd) {
            // 清理结束动画
            // 清理大圆
            if (mTrashGreatCircleListener != null) {
                int color = mTrashGreatCircleListener.setCleanBackgroundColor();
                mCleanInnerCirclepaint.setColor(color);
            }
            clean(canvas);
        } else if (isFinish) { //完成
            //更新外进度条
            RectF oval = new RectF(mHalfWidth - mOuterCircle, mHalfHeight - mOuterCircle, mHalfWidth + mOuterCircle,
                    mHalfHeight + mOuterCircle);
            canvas.drawArc(oval, 0, 360, false, mPaint);// 小弧形
            // 绘制内部
            canvas.drawCircle(mHalfWidth, mHalfHeight, mOuterCircle - mOneDP, mCleanInnerCirclepaint);
        } else if (isNeedCleanAgain) { //需要再次清理
            if (mTrashGreatCircleListener != null) {
                int color = mTrashGreatCircleListener.setCleanBackgroundColor();
                mCleanInnerCirclepaint.setColor(color);
            }
            cleanAgain(canvas);
        } else if (isReScan) { //再次扫描
            if (mProgressLength < 360) {
                updateOutProgress(canvas, mProgressLength);
                // 绘制星星
                drawTrashStar(canvas, scan(canvas));
                mProgressLength += mRescanSpeed;
                invalidate();
            } else {
                clean(canvas);
            }
        }
        // 绘制垃圾大小
        drawTrashSize(canvas, mTrashSize);
    }

    /**
     * 获取字体的高度
     */
    private float getFontHeight(Paint paint) {
        FontMetrics fm = paint.getFontMetrics();
        return (float) Math.ceil(fm.descent - fm.top);
    }

    /**
     * 动态获取绘制的宽高
     */
    private int[] getTextWidthHeight(Paint paint, String text) {
        Rect rect = new Rect();
        // 返回包围整个字符串的最小的一个Rect区域
        paint.getTextBounds(text, 0, text.length(), rect);
        int strwid = rect.width();
        int strhei = rect.height();
        return new int[]{strwid, strhei
        };
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mTotalWidth = w;
        mHalfWidth = (mTotalWidth - mOneDP) / 2;
        mTotalHeight = h;
        mHalfHeight = mTotalHeight / 2 + mOneDP;
        // 更新扫描扇形效果
        mProgressShader = getCircleShader();
        // 设置个新的长方形，扫描测量
        mShaderOval = new RectF(mHalfWidth - mShaderRaduls, mHalfHeight - mShaderRaduls - mOneDP / 2,
                mHalfWidth + mShaderRaduls + mOneDP, mHalfHeight + mShaderRaduls);
        mProgressOval = new RectF(mHalfWidth - mProgressRad, mHalfHeight - mProgressRad, mHalfWidth + mProgressRad,
                mHalfHeight + mProgressRad);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        // TODO 控制动画停止，将状态设置为不更新并控制当前状态。
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
                if (!obtainInlineCircle(x, y)) {
                    return true;
                }
                break;
        }
        return super.onTouchEvent(event);
    }

    /**
     * 判断屏幕上一点是否在内圆范围内。算法为：若该点距离圆心的距离小于内圆半径，则认为在圆内
     */
    private boolean obtainInlineCircle(float x, float y) {
        float xDistance = Math.abs(x - mHalfWidth);
        float yDistance = Math.abs(y - mHalfHeight);
        float location = (xDistance * xDistance + yDistance * yDistance);
        return location <= mInnerCircle * mInnerCircle;
    }

}
